<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'
import { getArticleList, deleteArticle } from '../api/article'

const router = useRouter()

const searchForm = ref({
  query: '',
  type: '',
  pageNum: 1,
  pageSize: 10
})

const tableData = ref([])
const total = ref(0)

const loadData = async () => {
  try {
    const res = await getArticleList(searchForm.value)
    tableData.value = res.data.list
    total.value = res.data.total
  } catch (error) {
    console.error('加载数据失败：', error)
  }
}

const handleSearch = () => {
  searchForm.value.pageNum = 1
  loadData()
}

const handleSizeChange = (val) => {
  searchForm.value.pageSize = val
  loadData()
}

const handleCurrentChange = (val) => {
  searchForm.value.pageNum = val
  loadData()
}

const handleEdit = (row) => {
  router.push(`/edit?id=${row.id}`)
}

const handleDelete = async (row) => {
  try {
    await ElMessageBox.confirm('确认删除该文章吗？', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    })
    await deleteArticle(row.id)
    ElMessage.success('删除成功')
    loadData()
  } catch (error) {
    if (error !== 'cancel') {
      console.error('删除失败：', error)
      ElMessage.error('删除失败')
    }
  }
}

onMounted(() => {
  loadData()
})
</script>

<template>
  <div class="article-list">
    <el-card class="box-card">
      <template #header>
        <div class="card-header">
          <span>文章列表</span>
          <el-button type="primary" @click="router.push('/edit')">新建文章</el-button>
        </div>
      </template>

      <el-form :inline="true" :model="searchForm" class="search-form">
        <el-form-item>
          <el-input v-model="searchForm.query" placeholder="请输入文章ID或标题" clearable @keyup.enter="handleSearch">
            <template #append>
              <el-button :icon="Search" @click="handleSearch" />
            </template>
          </el-input>
        </el-form-item>

        <el-form-item>
          <el-input v-model="searchForm.type" placeholder="请输入文章类型" clearable @keyup.enter="handleSearch" />

        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="handleSearch">搜索</el-button>
        </el-form-item>
      </el-form>

      <el-table :data="tableData" style="width: 100%" v-loading="loading">
        <el-table-column prop="id" label="ID" width="80" />
        <el-table-column prop="title" label="标题" width="200" />
        <el-table-column prop="updateTime" label="修改日期" width="180">
          <template #default="{ row }">
            {{ new Date(row.updateTime).toLocaleString() }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180">
          <template #default="{ row }">
            <!-- <el-button type="primary" link @click="handlePreview(row)">预览</el-button> -->
            <!-- <el-button :type="row.status === '已发布' ? 'success' : 'warning'" link>
              {{ row.status === '已发布' ? '上架' : '发布' }}
            </el-button> -->
          </template>
        </el-table-column>
        <el-table-column label="编辑" width="300" fixed="right">
          <template #default="{ row }">
            <el-button type="primary" link @click="router.push(`/pronunciation?id=${row.id}`)">发音</el-button>
            <el-button type="primary" link @click="router.push(`/image-edit?id=${row.id}`)">图片</el-button>
            <el-button type="primary" link @click="router.push(`/design?id=${row.id}`)">设计</el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination-container">
        <el-pagination v-model:current-page="searchForm.pageNum" v-model:page-size="searchForm.pageSize"
          :page-sizes="[10, 20, 50, 100]" :total="total" @size-change="handleSizeChange"
          @current-change="handleCurrentChange" layout="total, sizes, prev, pager, next, jumper" />
      </div>
    </el-card>
  </div>
</template>

<style scoped>
.article-list {
  height: 100%;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.search-form {
  margin-bottom: 20px;
}

.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}
</style>